package Basak;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;

public class Maven_Database {

	public static void main(String[] args) throws SQLException {
		Connection connection = null;
		Statement statement = null;
	    PreparedStatement preparedStatement = null;
	    ResultSet resultSet = null;
	    
	    
		String url = "jdbc:mysql://localhost/test";
		String user = "root";
		String password = "basak";

		try {
			//Class.forName("com.mysql.jdbc.Driver");
			
			connection=(Connection) DriverManager.getConnection(url, user, password);
			connection.setAutoCommit(false);
			statement=(Statement) connection.createStatement();
			preparedStatement = (PreparedStatement) connection.prepareStatement("insert into  test.books values (?, ?, ?, ?, ? , ?, ?,?,?)");
			 preparedStatement.setInt(1,6);
		      preparedStatement.setString(2, "gg");
		      preparedStatement.setDate(3, new java.sql.Date(2009, 12, 11));
		     preparedStatement.setInt(4,2);
		      preparedStatement.setString(5, "aa");
		      preparedStatement.setString(6, "Tisbn");
		      preparedStatement.setString(7, "Tisbn");
		      preparedStatement.setInt(8, 5);
		      preparedStatement.setString(9, "Tisbn");
		      preparedStatement.executeUpdate();
		      connection.commit();
		      preparedStatement = (PreparedStatement) connection.prepareStatement("SELECT * from test.books");
		      resultSet = preparedStatement.executeQuery();
		      writeResultSet(resultSet);
			resultSet = statement.executeQuery("select * from test.articles");
		    writeResultSet(resultSet);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
        finally{
        if(statement!=null) statement.close();
        if (connection!=null) connection.close();
        
       
        }
        }

	private static void writeResultSet(ResultSet resultSet) throws SQLException {
		 while (resultSet.next()) {
			 int objId = resultSet.getInt("objid");
		      String issueArea = resultSet.getString("issue_area");
		      Date date = resultSet.getDate("issue_date");
		      int authorCount = resultSet.getInt("author_count");
		      String bookName = resultSet.getString("book_name");
		      String authorName = resultSet.getString("author_name");
		      String ISBN = resultSet.getString("isbn_no");
		      String publisherName = resultSet.getString("publisher_name");
		      
		      System.out.println("ObjectID: " + objId);
		      System.out.println("Issue Area: " + issueArea);
		      System.out.println("Author Count: " + authorCount);
		      System.out.println("Date: " + date);
		      System.out.println("Author NAme: " + authorName);
		      System.out.println("Book NAme: " + bookName);
		      System.out.println("ISBN: " + ISBN);
		      System.out.println("Publisher NAme: " + publisherName);
			 
		 }
		
	}
	}

